
var TaskDialog = new Object();

TaskDialog.createView = function(mode, task) {
	var dialog = WindDialog.createView();
	dialog.setId("taskDialog");
		
	if(mode=="insert") {
		dialog.setTitle("Insert a new task");
	} else if(mode=="update") {
		dialog.setTitle("Update an existing task");
	}
	
	if(mode=="insert") {
		var button = WindDialog.createToolbarButtonView();
		button.setText("Insert");
		button.onClick = function() {
			var task = new TaskDTO();
			task.setTitle(titleText.getValue());
			task.setDescription(descriptionText.getValue());
			task.setStartDate(startDate.getValue());
			task.setDuration(durationNumber.getValue());
			task.setColor(representationColor.getValue());
			var result = WindData.execute("insertTask", task);
			dialog.close("inserted");
		}
		dialog.getToolbar().addView(button);
	}
	
	if(mode=="update") {
		var button = WindDialog.createToolbarButtonView();
		button.setText("Update");
		button.onClick = function() {
			var modifiedTask = new TaskDTO();
			modifiedTask.setTitle(titleText.getValue());
			modifiedTask.setDescription(descriptionText.getValue());
			modifiedTask.setStartDate(startDate.getValue());
			modifiedTask.setDuration(durationNumber.getValue());
			modifiedTask.setColor(representationColor.getValue());
			var result = WindData.execute("updateTask", modifiedTask);
			dialog.close("updated");
		}
		dialog.getToolbar().addView(button);
	}
	
	{
		var button = WindDialog.createToolbarButtonView();
		button.setText("Cancel");
		button.onClick = function() {
			dialog.close();
		}
		dialog.getToolbar().addView(button);
	}
	
	{
		var label = WindLabel.createView();
		label.setId("titleLabel");
		label.setText("Title:"); 
		dialog.getBody().addView(label);
	}
	
	var titleText = WindText.createView();
	titleText.setId("titleText");
	if(task!=null) {
		titleText.setValue(task.getTitle());
	}
	dialog.getBody().addView(titleText);
	
	var startTypeDateRadio = WindRadio.createView();
	startTypeDateRadio.setId("startTypeDateRadio");
	startTypeDateRadio.setName("startType");
	if(task!=null) {
		startTypeDateRadio.setValue("true");
	}
	dialog.getBody().addView(startTypeDateRadio);
	
	{
		var label = WindLabel.createView();
		label.setId("startsLabel");
		label.setText("This task starts:"); 
		dialog.getBody().addView(label);
	}
	
	{
		var label = WindLabel.createView();
		label.setId("startsAtLabel");
		label.setText("at:"); 
		dialog.getBody().addView(label);
	}
	
	var startDate = WindDate.createView();
	startDate.setId("startDate");
	if(task!=null) {
		startDate.setValue(task.getStartDate());
	}
	dialog.getBody().addView(startDate);
	
	var startTypePreviousTaskRadio = WindRadio.createView();
	startTypePreviousTaskRadio.setId("startTypePreviousTaskRadio");
	startTypePreviousTaskRadio.setName("startType");
	if(task!=null) {
		startTypePreviousTaskRadio.setValue("false");
	}
	dialog.getBody().addView(startTypePreviousTaskRadio);
	
	{
		var label = WindLabel.createView();
		label.setId("startsWhenTheTaskLabel");
		label.setText("when the task:"); 
		dialog.getBody().addView(label);
	}
	
	{
		var taskSelector = TaskSelector.createView();
		taskSelector.setId("taskSelector");
		dialog.getBody().addView(taskSelector);
	}
	
	{
		var label = WindLabel.createView();
		label.setId("startsWhenThePreviousTaskDidFinishLabel");
		label.setText("did finish"); 
		dialog.getBody().addView(label);
	}
	
	{
		var label = WindLabel.createView();
		label.setId("finishesLabel");
		label.setText("This task finishes:"); 
		dialog.getBody().addView(label);
	}
	
	var finishTypeDateRadio = WindRadio.createView();
	finishTypeDateRadio.setId("finishTypeDateRadio");
	finishTypeDateRadio.setName("finishType");
	if(task!=null) {
		finishTypeDateRadio.setValue("false");
	}
	dialog.getBody().addView(finishTypeDateRadio);
	
	{
		var label = WindLabel.createView();
		label.setId("finishesAtLabel");
		label.setText("at:"); 
		dialog.getBody().addView(label);
	}
	
	{
		var date = WindDate.createView();
		date.setId("finishDate");
		dialog.getBody().addView(date);
	}
	
	var finishTypeDurationRadio = WindRadio.createView();
	finishTypeDurationRadio.setId("finishTypeDurationRadio");
	finishTypeDurationRadio.setName("finishType");
	if(task!=null) {
		finishTypeDurationRadio.setValue("true");
	}	
	dialog.getBody().addView(finishTypeDurationRadio);
	
	{
		var label = WindLabel.createView();
		label.setId("finishesWhenLabel");
		label.setText("when:"); 
		dialog.getBody().addView(label);
	}
	
	{
		var label = WindLabel.createView();
		label.setId("finishesWhenDurationMinutesEllapsedLabel");
		label.setText("minutes ellapsed"); 
		dialog.getBody().addView(label);
	}
	
	var durationNumber = WindNumber.createView();
	durationNumber.setId("durationNumber");
	if(task!=null) {
		durationNumber.setValue(task.getDuration());
	}
	dialog.getBody().addView(durationNumber);
	
	{
		var label = WindLabel.createView();
		label.setId("colorLabel");
		label.setText("Represented by color:"); 
		dialog.getBody().addView(label);
	}

	var representationColor = WindNumber.createView();
	representationColor.setId("representationColor");
	if(task!=null) {
		representationColor.setValue(task.getColor());
	}
	dialog.getBody().addView(representationColor);
	
	{
		var label = WindLabel.createView();
		label.setId("descriptionLabel");
		label.setText("Description:"); 
		dialog.getBody().addView(label);
	}
	
	var descriptionText = WindText.createView();
	descriptionText.setId("descriptionText");
	descriptionText.setLines(10);
	if(task!=null) {
		descriptionText.setValue(task.getDescription());
	}
	dialog.getBody().addView(descriptionText);
	
	return dialog;
}

